package functionInterface;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author mubei
 * @date 2021/11/21
 * @description
 * 测试函数型接口
 *
 * 消费型接口  Consumer<T>    void accept(T t)
 * 供给型接口  Supplier<T>   T get()
 * 函数型接口  Function<T,R>  R apply(T t)
 * 断定型函数接口 Predicate<T>  boolean test(T t)
 */
public class TestFunction {

    //消费接口
    @Test()
    public void test1() {
        happyTime(50, d-> System.out.println("学习太累了  shopping去  花了"+d));
    }

    public void happyTime(double money, Consumer<Double> con){
        con.accept(money);
    }


    // 测试断定函数接口
    @Test()
    public void test2() {
        List<String> list = Arrays.asList("北京","慧宝", "南京", "东京", "普京");

        filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });

        ArrayList<String> strings = filterString(list, s -> s.contains("宝"));
        for (String s : strings) {
            System.out.println(s);
        }


    }

    // 根据规则  过滤字符串
    public  ArrayList<String> filterString(List<String> list, Predicate<String> pre){
        ArrayList<String> arrayList = new ArrayList<>();

        for (String s : list) {
            if(pre.test(s)){
                arrayList.add(s);
            }
        }
        return arrayList;
    }
    
    
    //测试函数型接口
    @Test()
    public void test3() {
        System.out.println(replcae("ab", (s) -> -1));
    }

    public Integer replcae(String s,Function<String,Integer> f){
        return f.apply(s);
    }

    // 测试供给型接口
    @Test()
    public void test4() {
        get(()->1024);
    }

    public Integer get(Supplier<Integer> sup){
        System.out.println(sup.get());
        return 2333;
    }

}
